<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 4/24/19
 * Time: 4:45 PM
 */

namespace app\admin\controller;

use think\Request;
use app\admin\model\TraceSourceModel;

class Tracesource
{
    /**
     * 添加廠商追溯信息
     * @return \think\response\Json
     */
    public function addTraceSource(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $result = [];
        $data = $request->post();
        $num = TraceSourceModel::addTraceSource($data);
        if ($num > 0){
            $result["code"] = 200;
            $result["msg"] = "添加商家追溯成功！";
            return json($result);
        }else{
            $result["code"] = 400;
            $result["msg"] = "添加商家追溯失敗！";
            return json($result);
        }
    }

    /**
     * 更新廠家追溯信息
     * @return \think\response\Json
     */
    public function updateTraceSourceById(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $result = [];
        $data = $request->post();
        $num = TraceSourceModel::updateTraceSourceById($data);
        if ($num > 0){
            $result["code"] = 200;
            $result["msg"] = "更新商家追溯成功！";
            return json($result);
        }else{
            $result["code"] = 400;
            $result["msg"] = "更改失败！请检查商品ID是否存在！！！";
            return json($result);
        }
    }

    /**
     * 批量刪除廠家追溯信息
     * @return \think\response\Json
     */
    public function deleteTraceSourceById(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $tasIdList = explode(",",$data["tas_id"]);
        $num = count($tasIdList);
        $deleteNum = 0;

        $msg = array();
        for ($x=0; $x<$num; $x++){
            $a = TraceSourceModel::deleteTraceSourceById($tasIdList[$x]);
            if ($a == 0){
                $msg["code"] = 400;
                $msg["deleteNum"] = "已刪除".$deleteNum."條，id：".$tasIdList[$x]."不存在";
                $msg["msg"] = "刪除異常！";
                return json($msg);
            }

            $deleteNum = $deleteNum + $a;
        }
        $msg["code"] = 200;
        $msg["deleteNum"] = "已刪除".$deleteNum."條";
        $msg["msg"] = "刪除成功！";

        return json($msg);
    }

    /**
     * 根據商品ID查詢廠家追溯信息
     * @return \think\response\Json
     */
    public function selectAllTraceSourceById(){
        $request = Request::instance();
        $data = $request->param();
        $content = array();
        $data = TraceSourceModel::selectAllTraceSourceById($data["co_id"]);
        $content["code"] = 200;
        $content["msg"] = $data;
        return json($content);
    }
}